package com.phone.modules.rms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.phone.modules.pms.model.PmsProduct;
import com.phone.modules.pms.service.PmsProductService;
import com.phone.modules.rms.dto.RmsHeatResultDTO;
import com.phone.modules.rms.dto.RmsQueryDTO;
import com.phone.modules.rms.model.RmsCharge;
import com.phone.modules.rms.model.RmsHeat;
import com.phone.modules.rms.mapper.RmsHeatMapper;
import com.phone.modules.rms.service.RmsHeatService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 性能发热排行表 服务实现类
 * </p>
 *
 * @author WangHao
 * @since 2022-04-18
 */
@Service
public class RmsHeatServiceImpl extends ServiceImpl<RmsHeatMapper, RmsHeat> implements RmsHeatService {
    @Autowired
    PmsProductService productService;

    @Autowired
    RmsHeatMapper heatMapper;
    /**
     * 前台展示-性能发热信息
     * @param queryDTO
     * @return
     */
    @Override
    public Page searchList(RmsQueryDTO queryDTO) {
        List<RmsHeatResultDTO> resultDTOList = heatMapper.searchList(queryDTO);
        if (Objects.equals(queryDTO.getOrder(), "tab1")) {
            // 如果是tab1，需要对20款应用两轮打开时间进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsHeatResultDTO::getOpenTime));
            for (RmsHeatResultDTO heatResultDTO : resultDTOList) {
                heatResultDTO.setPer(Double.valueOf(resultDTOList.get(resultDTOList.size()-1).getOpenTime()));
            }
        } else if (Objects.equals(queryDTO.getOrder(), "tab2")) {
            // 如果是tab2，需要对半小时原神最高画质机身最高温度进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsHeatResultDTO::getGenshinHeat));
            for (RmsHeatResultDTO heatResultDTO : resultDTOList) {
                heatResultDTO.setPer(resultDTOList.get(resultDTOList.size()-1).getGenshinHeat());
            }
        } if (Objects.equals(queryDTO.getOrder(), "tab3")) {
            // 如果是tab3，需要对一小时和平精英最高画质机身最高温度进行排序，正序
            resultDTOList.sort(Comparator.comparing(RmsHeatResultDTO::getHpHeat));
            for (RmsHeatResultDTO heatResultDTO : resultDTOList) {
                heatResultDTO.setPer(resultDTOList.get(resultDTOList.size()-1).getHpHeat());
            }
        }
        Page page = new Page(queryDTO.getPageNum(), queryDTO.getPageSize());
        page.setRecords(resultDTOList);
        page.setTotal(heatMapper.getCount(queryDTO));
        return page;
    }

    /**
     * 分页获取性能发热信息列表
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page listHeat(String keyword, Integer pageNum, Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        QueryWrapper<RmsHeat> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.lambda().like(RmsHeat::getProductName, keyword);
        }
        // 排序
        queryWrapper.lambda().orderByAsc(RmsHeat::getOpenTime);
        return this.page(page, queryWrapper);
    }

    /**
     * 新增性能发热信息
     * @param heat
     * @return
     */
    @Override
    public boolean saveHeat(RmsHeat heat) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PmsProduct::getProductName, heat.getProductName());
        PmsProduct product = productService.getOne(queryWrapper);
        if (product != null) {
            heat.setProductId(product.getId());
        }
        return this.save(heat);
    }

    /**
     * 更新性能发热信息
     * @param id
     * @param heat
     * @return
     */
    @Override
    public boolean updateHeat(Long id, RmsHeat heat) {
        heat.setId(id);
        return this.updateById(heat);
    }

    /**
     * 批量删除性能发热信息
     * @param ids
     * @return
     */
    @Override
    public boolean deleteHeat(List<Long> ids) {
        return removeByIds(ids);
    }

}
